package com.herui.iot.ui.view.ecg.buff;

import com.herui.sport_care.config.ECGConfig;

import java.util.ArrayList;


public class MultiChannelBufferBT {

    private static final String TAG = "MultiChannelBufferBT";
    private int frequency = 1000;
    private boolean frequencyChange = false;
    private int secondBuffer = 20;
    private ArrayList<int[]> bufferList;

    private int currentChannel;
    private int currentIndex;

    long startT, lastT;

    public MultiChannelBufferBT() {
        int MaxElements = frequency * secondBuffer;
        currentChannel = 0;
        currentIndex = 0;

        startT = 0;
        // connectStr = "http://localhost:8787";
        // create and initialize mutli-channel buffer
        bufferList = new ArrayList<int[]>();
        for (int indexC = 0; indexC < ECGConfig.ECGCHANNELTOTAL; ++indexC) {
            int[] tempIntArray = new int[MaxElements];
            for (int j = 0; j < secondBuffer; ++j) {
                int startP = j * frequency;
                for (int i = startP; i < (startP + frequency); ++i) {
                    tempIntArray[i] = 32768;

                }
            }
            bufferList.add(tempIntArray);
        }
    }

    public ArrayList<int[]> getBufferList() {
        return bufferList;
    }

    public int getLocalMeanChannel(int start, int width, int Channel) {
        int maxElement = frequency * secondBuffer;
        if (start < 0)
            start += maxElement;
        start = start % (maxElement);
        int meanV = 0;
        for (int i = start; i < (start + width); ++i) {
            meanV += bufferList.get(Channel)[i % maxElement];
        }
        return meanV / width;
    }

    public boolean isRunning() {
        long cur = System.currentTimeMillis();
        if (cur - lastT > 5000)
            return false;
        return true;
    }

    public int getFrequency() {
        return frequency;
    }

    public void setFrequency(int _frequency) {
        if (_frequency == 0)
            _frequency = 300;

        if (frequency == _frequency)
            return;
        frequency = _frequency;
        currentIndex = 0;
        frequencyChange = true;
    }

    public boolean isFrequencyChange() {
        boolean isChange = frequencyChange;
        frequencyChange = false;
        return isChange;
    }

    public int getTimeWindow() {
        return secondBuffer;
    }

    public void putECGDataBuffer(int[][] buffer) {
        lastT = System.currentTimeMillis();
        for (int[] block : buffer) {
            putECGDataBuffer(block);

            currentIndex++;
            currentIndex = currentIndex % (frequency * secondBuffer);
        }
    }

    private int temp;
    private int pre;

    private void putECGDataBuffer(int[] block) {
        if (temp != block[0]) {
            temp = block[0];
        }

        bufferList.get(0)[currentIndex] = block[0];
        bufferList.get(1)[currentIndex] = block[0];
    }

    public int getCurrentPT() {
        return currentIndex;
    }

    public int getSecondBuffer() {
        return secondBuffer;
    }

    public void setSecondBuffer(int _secondBuffer) {
        secondBuffer = _secondBuffer;
    }


}
